package simportal.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import simportal.service.NavMenuService;

import com.cfuture08.eweb4j.component.DivPageComp;
import com.cfuture08.eweb4j.component.dwz.DWZUtil;
import com.cfuture08.eweb4j.component.dwz.vo.NavMenu;
import com.cfuture08.eweb4j.mvc.annotation.Controller;
import com.cfuture08.eweb4j.mvc.annotation.Param;
import com.cfuture08.eweb4j.mvc.annotation.RequestMapping;
import com.cfuture08.eweb4j.mvc.annotation.Result;
import com.cfuture08.eweb4j.mvc.annotation.Singleton;
import com.cfuture08.eweb4j.mvc.annotation.ValField;
import com.cfuture08.eweb4j.mvc.annotation.Validator;
import com.cfuture08.util.JsonConverter;

@Controller
@Singleton
@RequestMapping("admin/navMenus")
public class NavMenuControl {
	// 导航菜单服务类
	private static final NavMenuService service = new NavMenuService();
	private static final String model = "navMenus";
	private static String success = DWZUtil.getSuccessJson("操作成功", "xssydhcd",
			"navMenus/list", "reloadTab", "显示所有导航菜单").toString();

	/**
	 * GET|POST方式,返回所有导航菜单列表，json格式
	 * 
	 * @param out
	 * @return
	 */
	@RequestMapping(value = "/suggest.json", method = "GET|POST")
	public String suggestJson(PrintWriter out) {
		out.print(JsonConverter.convert(service.getAll()));
		return null;
	}

	/**
	 * 批量删除
	 * 
	 * @param ids
	 * @param out
	 * @return
	 */
	@RequestMapping(value = "/batchRemove", method = "DELETE", showValErr = "dwzJson")
	@Validator("required")
	@ValField(name = "ids", message = "请选择要删除的信息")
	public String batchRemoveHandler(@Param("ids") Integer[] ids,
			PrintWriter out) {
		String info = null;
		String error = service.batchRemove(ids);
		if (error == null)
			info = success;
		else
			info = DWZUtil.getFailedJson(error).toString();
		out.print(info);
		return null;
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @param out
	 * @return
	 */
	@RequestMapping(value = "/{id}", method = "DELETE", showValErr = "dwzJson")
	@Validator("required")
	@ValField(name = "id", message = "请选择要删除的信息")
	public String removeHandler(@Param("id") Integer id, PrintWriter out) {
		return this.batchRemoveHandler(new Integer[] { id }, out);
	}

	/**
	 * 列表显示
	 * 
	 * @param p
	 * @param n
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/list", method = "GET|POST")
	@Result(name = { "success" }, location = { "list.jsp" })
	public String listHandler(@Param(value = "pageNum", init = "1") int p,
			@Param(value = "numPerPage", init = "20") int n,
			HttpServletRequest request) {
		List<NavMenu> pojos = service.getPage(p, n);
		if (pojos != null) {
			request.setAttribute("model", model);
			request.setAttribute("pojos", pojos);
			// 分页组件
			request.setAttribute("dpc",
					new DivPageComp(p, n, service.countAll(), 10));
			request.setAttribute("random", Math.random());
		}
		return "success";
	}

	/**
	 * 关键字查询
	 * 
	 * @param keyword
	 * @param p
	 * @param n
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/search", method = "GET|POST")
	@Result(name = "success", location = "list.jsp")
	public String searchKeyword(@Param("keyword") String keyword, int p, int n,
			HttpServletRequest request) {
		request.setAttribute("model", model);
		request.setAttribute("pojos", service.searchByKeywordAndPaging(
				new String[] { "name" }, keyword, p, n, 0));
		request.setAttribute("keyword", keyword);
		List<NavMenu> list = service.searchByKeywordAndPaging(new String[] {
				"name", "href" }, keyword, -1, -1, 0);
		long count = 0;
		if (list != null) {
			count = list.size();
		}
		// 分页组件
		request.setAttribute("dpc", new DivPageComp(p, n, count, 10));
		request.setAttribute("random", Math.random());
		return "success";
	}

	/**
	 * 分页显示，数据返回到lookup.jsp中
	 * 
	 * @param p
	 * @param n
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/lookup", method = "GET|POST")
	@Result(name = { "success" }, location = { "lookup.jsp" })
	public String lookupHandler(@Param(value = "pageNum", init = "1") int p,
			@Param(value = "numPerPage", init = "20") int n,
			HttpServletRequest request) {
		return this.listHandler(p, n, request);
	}

	/**
	 * 查询关键字，数据返回到lookup.jsp页面中
	 * 
	 * @param keyword
	 * @param p
	 * @param n
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/lookupSearch", method = "GET|POST")
	@Result(name = { "success" }, location = { "lookup.jsp" })
	public String lookupKeyword(@Param("keyword") String keyword,
			@Param(value = "pageNum", init = "1") int p,
			@Param(value = "numPerPage", init = "20") int n,
			HttpServletRequest request) {
		return this.searchKeyword(keyword, p, n, request);
	}

	/**
	 * GET|POST方式，跳转到添加页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/new", method = "GET|POST")
	public String addHandler(HttpServletRequest request) {
		request.setAttribute("model", model);
		return "add.jsp";
	}

	/**
	 * 添加
	 * 
	 * @param pojo
	 * @param out
	 * @return
	 */
	@RequestMapping(value = "/", method = "POST", showValErr = "dwzJson")
	@Validator("int")
	@ValField(message = "排序项只能输入数字", name = "rank")
	public String createHandler(NavMenu pojo, PrintWriter out) {
		String info = null;
		String error = service.create(pojo, "name", "菜单名");
		if (error == null)
			info = success;
		else
			info = DWZUtil.getFailedJson(error).toString();
		out.print(info);
		return null;
	}

	/**
	 * GET|POST方式打开编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/{id}/edit", method = "GET|POST", showValErr = "dwzJson")
	@Validator("required")
	@ValField(message = { "请选择要编辑的信息" }, name = { "id" })
	public String editHandler(@Param("id") Integer id,
			HttpServletRequest request) {
		NavMenu pojo = service.getOne(id);
		if (pojo != null) {
			request.setAttribute("pojo", pojo);
			request.setAttribute("model", model);
		}
		return "../edit.jsp";
	}

	/**
	 * 修改
	 * 
	 * @param pojo
	 * @param out
	 * @return
	 */
	@RequestMapping(value = "/{navMenuId}", method = "PUT", showValErr = "dwzJson")
	@Validator(value = { "required", "int" })
	@ValField(index = { 0, 1 }, message = { "请选择要编辑的信息", "排序项只能输入数字" }, name = {
			"navMenuId", "rank" })
	public String updateHandler(NavMenu pojo, PrintWriter out) {
		String info = null;
		String error = service.update(pojo, "name", "菜单名");
		if (error == null)
			info = success;
		else
			info = DWZUtil.getFailedJson(error).toString();
		out.print(info);
		return null;
	}
}
